package com.bds.tjj.service;

import com.bds.tjj.model.ConfidenData;
import com.bds.tjj.model.PropIndex;
import com.bds.tjj.utils.Const;
import org.nutz.dao.Cnd;
import org.nutz.dao.impl.NutDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.URLDecoder;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by guoyu on 2018/1/29.
 */
@Service
public class ConfidenSumService {
    private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(ConfidenSumService.class);
    private static final DecimalFormat df = new DecimalFormat("#.0");

    @Resource
    private NutDao dao;

    public ConfidenData getConfidenMonthNum(String type) throws Exception {
        type = URLDecoder.decode(type, "utf-8");
        List<ConfidenData> confidenDataList = dao.query(ConfidenData.class,
                Cnd.where("prop", "=", type).limit(1, 2).orderBy("ym", "desc"));

        ConfidenData confidenData = confidenDataList.get(0);
        ConfidenData confidenDataAgo = confidenDataList.get(1);
        PropIndex pi = dao.fetch(PropIndex.class, Cnd.where("prop", "=", type));
        if (pi != null) {
            confidenData.setUnit(pi.getUnit());
        }

        Integer ym = confidenData.getYm();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMM");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM");
        if (ym != null) {
            Date yearMonth = sdf1.parse(ym + "");
            String vym = sdf2.format(yearMonth);
            confidenData.setView_ym(vym);
        }
        if (confidenData.getData() != null && confidenData.getData().contains(".")) {
            // 小数点后是否大于4位
            if (confidenData.getData().split("\\.")[1].length() > Const.decimal_num) {
                confidenData.setData(df.format(Double.parseDouble(confidenData.getData())));
            }
        }

        //计算IYA
        try {
            Integer preYm = confidenData.getYm() - 100;
            ConfidenData preYmData = dao.fetch(ConfidenData.class, Cnd.where("prop", "=", type)
                    .and("data", "is not", null).and("data", "<>", "")
                    .and("ym", "=", preYm));

            if (preYmData != null) {
                if (confidenData.getData() != null && (!confidenData.getData().equals(""))) {
                    double a = Double.parseDouble(confidenData.getData()) - Double.parseDouble(preYmData.getData());
                    double b = Math.abs(Double.parseDouble(preYmData.getData()));
                    double r = (a * 1.0 / b) * 100;
                    confidenData.setIya(Double.parseDouble(df.format(r)));
                } else {
                    confidenData.setIya(null);
                }
            } else {
                confidenData.setIya(null);
            }
        } catch (Exception e) {
            confidenData.setIya(null);
        }

        //计算ipp
        if (confidenData.getYm() - confidenDataAgo.getYm() == 1) {
            confidenData.setAbs(Double.parseDouble(
                    df.format(
                            Double.parseDouble(confidenData.getData()) - Double.parseDouble(confidenDataAgo.getData()))));
            double value = Double.parseDouble(df
                    .format((Double.parseDouble(confidenData.getData()) - Double.parseDouble(confidenDataAgo.getData()))
                            * 1.0 / Math.abs(Double.parseDouble(confidenDataAgo.getData()))));
            if (value == 0) {
                confidenData.setYya(0.0);
                confidenData.setIpp(0.0);
            } else {
                confidenData.setYya(value);
                confidenData.setIpp(value);
            }
        } else {
            confidenData.setYya(null);
            confidenData.setIpp(null);
            confidenData.setAbs(null);
        }

        return confidenData;
    }

    public List<ConfidenData> getConfidenTrend(String type, int startTime, int endTime) throws Exception {
        type = URLDecoder.decode(type, "utf-8");
        List<ConfidenData> list = dao.query(ConfidenData.class,
                Cnd.where("prop", "=", type).and("ym", ">=", startTime).and("ym", "<=", endTime).orderBy("ym", "desc"));

        for (ConfidenData confidenData : list) {
            if (confidenData.getData() == null) {
                continue;
            }
            PropIndex prop = dao.fetch(PropIndex.class, Cnd.where("prop", "=", confidenData.getProp()));
            if (prop != null) {
                confidenData.setEn_name(prop.getEn_name());
                confidenData.setIs_area(prop.getIs_area());
            }
            confidenData.setView_name(prop.getView_name());
            if (confidenData.getData().contains(".")) {
                // 小数点后是否大于4位
                if (confidenData.getData().split("\\.")[1].length() > Const.decimal_num) {
                    confidenData.setData(df.format(Double.parseDouble(confidenData.getData())));
                }
            }
        }

        return list;
    }

}
